<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="回款编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['receivableCode',validatorRules.receivableCode]"
                placeholder="请输入回款编号"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="客户" v-if="customerId && !model.id" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="true"
                type="list"
                v-decorator="['customerId',validatorRules.customerId]"
                :dictCode="'CrmCustomer,customerName,id,ownerUserId='+ownerUserId"
              />
            </a-form-item>
            <a-form-item label="客户" v-else :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['customerId',validatorRules.customerId]"
                :trigger-change="true"
                :dictCode="'CrmCustomer,customerName,id,ownerUserId='+ownerUserId"
                placeholder="请选择客户"
              />
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="合同" v-if="contractId && !model.id" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractId',validatorRules.contractId]" :disabled="true"></a-input>
            </a-form-item>
            <a-form-item label="合同" v-else :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['contractId',validatorRules.contractId]"
                :trigger-change="true"
                :dictCode="'CrmContract,name,id,ownerUserId='+ownerUserId"
                placeholder="请选择合同"
              />
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="回款日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                :disabled="disableSubmit"
                placeholder="请选择回款日期"
                v-decorator="['returnTime']"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="回款方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['returnType']"
                :trigger-change="true"
                dictCode="DataDictionary,description,dictionaryCode,dictionaryTypeId = RECEIVE_RETURNTYPE"
                placeholder="请选择回款方式"
              ></j-dict-select-tag>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="回款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['money', validatorRules.money]" placeholder="请输入回款金额"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea :disabled="disableSubmit" v-decorator="['remark']" rows="4" placeholder="请输入备注" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { mapGetters } from 'vuex'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'

export default {
  name: 'CrmReceivablesModal',
  components: {
    JDate,
    JDictSelectTag,
  },
  props: {
    customerId: {
      type: String,
      default: '',
    },
    contractId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      ownerUserId: this.userInfo().userLoginId,
      disableSubmit: false,
      form: this.$form.createForm(this),
      title: '操作',
      width: 1000,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        receivableCode: {
          rules: [{ required: true, message: '请输入回款编号!' }],
        },
        contractId: {
          rules: [{ required: true, message: '请选择合同!' }],
        },
        customerId: {
          rules: [{ required: true, message: '请选择客户!' }],
        },
       money: {
          rules: [
            { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额!' },],
        },
      },
      url: {
        add: '/online/api/createCrmReceivables',
        edit: '/online/api/updateCrmReceivables',
        get: '/online/api/getCrmReceivables',
      },
    }
  },
  created() {},
  methods: {
    ...mapGetters(['userInfo']),
    add() {
      this.edit({})
    },
    async edit(record) {
      this.form.resetFields()
      this.visible = true
      this.model = {}
      if (this.customerId) {
        this.model.customerId = this.customerId
      }
      if (this.contractId) {
        this.model.contractId = this.contractId
      }

      if (record.id) {
        const res = await httpAction(this.url.get, { id: record.id }, 'post')
        if (res.success) {
          this.model = Object.assign({}, res.result)
        }
      }
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'receivableCode',
            'planId',
            'customerId',
            'contractId',
            'checkStatus',
            'examineRecordId',
            'returnTime',
            'returnType',
            'money',
            'remark',
            'createUserId',
            'ownerUserId',
            'createTime',
            'updateTime',
            'batchId'
          )
        )
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                if (that.model.id) {
                  that.$emit('loadDetailInfo', that.model)
                }
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'receivableCode',
          'planId',
          'customerId',
          'contractId',
          'checkStatus',
          'examineRecordId',
          'returnTime',
          'returnType',
          'money',
          'remark',
          'createUserId',
          'ownerUserId',
          'createTime',
          'updateTime',
          'batchId'
        )
      )
    },
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-modal-mask {
  z-index: 98;
  background-color: rgb(245, 246, 249);
}
/deep/ .ant-modal-wrap {
  z-index: 99;
}
/deep/ .ant-modal-title {
  color: #333;
  font-weight: 700;
  font-size: 17px;
}
</style>